package com.eeepay.eeepay_v2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by zw on 2015/11/26.
 * 自动布局
 */
public class FlowLayout extends ViewGroup {


//    private static final String TAG = "print";

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

//    /**
//     * 绘制控件内容
//     * @param canvas
//     */
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//    }

    /**
     * 指定这个自定义布局所属的LayoutParams
     * @param
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    /**
     * 测量控件大小
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);


        int width = 0;//布局的总宽度
        int height = 0;//布局的总高度
        int lineWidth = 0;//当前行的宽度
        int lineHeight = 0;//当前行的高度

        //测量所有子控件的宽高
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        for(int i = 0; i < getChildCount(); i++){
            View view = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
            //控件所要占的宽度
            int vWidth = view.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            //控件所要占的高度
            int vHeight = view.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;

            if(lineWidth + vWidth <= wsize){
                //不需要换行
                lineWidth += vWidth;
                lineHeight = Math.max(lineHeight, vHeight);
            } else {
                //需要换行
                width = Math.max(width, lineWidth);
                height += lineHeight;
                lineWidth = vWidth;
                lineHeight = vHeight;
            }

            if(i == getChildCount() - 1){
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }

//            Log.d(TAG, "onMeasure() returned: " + width + "    " + height);
        }

        setMeasuredDimension(wmode == MeasureSpec.EXACTLY?wsize:width, hmode == MeasureSpec.EXACTLY?hmode:height);
    }

    /**
     * 摆放子控件位置
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = 0;//当前行的宽度
        int heigth = 0;//当前计算的总高度
        int lineHeight = 0;//当前行的最大高度
        for(int i = 0; i < getChildCount(); i++){
            View view = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
            //控件本身的宽度
            int vWidth = view.getMeasuredWidth();
            //控件本身的高度
            int vHeight = view.getMeasuredHeight();

            if(width + vWidth + layoutParams.leftMargin + layoutParams.rightMargin > getWidth()){
                //表示换行
                heigth += lineHeight;
                width = 0;
                lineHeight = 0;
            }

            l = width + layoutParams.leftMargin;
            t = heigth + layoutParams.topMargin;
            r = width + layoutParams.leftMargin + vWidth;
            b = heigth + layoutParams.topMargin + vHeight;

            width += (layoutParams.leftMargin + vWidth + layoutParams.rightMargin);
            lineHeight = Math.max(lineHeight, vHeight + layoutParams.topMargin + layoutParams.bottomMargin);

            view.layout(l, t, r, b);
        }
    }
}
